<?php
class classified extends table
{
	protected $_name	= 'my_classified';
	protected $fields	= array('id','classid','writer','type','flag','title','pic','litpic','keyword','info','content','contact','tel','qq','email','location','address','passdate','ip','posttime','updatetime','audit');
	protected $_primary = 'id';
	
	public function columnList($arg)
	{
		$show       = $arg['show']	= isset($arg['show']) ? abs($arg['show']) : 20;
        $page       = isset($arg['page']) ? abs($arg['page']) : 1;
        $order      = isset($arg['order']) ? $this->_name.'.'.$arg['order'] : $this->_name.'.id desc';
        
    	if ( isset($arg['field']) )
        {
            $where      	= array('classid'=>$arg['classid'],'field'=>$arg['field'],'value'=>$arg['value'],'order'=>$order,'limit'=>"$page,$show");
            $query      	= $this->searchQuery($where);
            $num        	= $this->getNum($query);
            $classifieds	= $this->getAll($query);
        }
        else 
        {
            $classifieds	= $this->fetchAll(array('classid'=>$arg['classid']),$order,$page,$show);
            $num        	= $this->getCount(array('classid'=>$arg['classid']));
        }
        return array($classifieds,$num);
	}
	
	public function fetchAll($where,$order=null,$count=null,$offset=null)
	{
		$list	= parent::fetchAll($where,$order,$count,$offset);
		foreach ($list as $key=>$val)
		{
			if ($val['flag']) $list[$key]['flag'] = explode(',',$val['flag']);
			$list[$key]['pic']		= !empty($list[$key]['pic']) ? explode('::::::', $list[$key]['pic']) : '';
			#$list[$key]['flag']	= $row['flag']	= explode(',',$row['flag']);
		}
		return $list;
	}
	
	public function getAllById($arg)
	{
		return parent::getAll(array('id'=>$arg));
	}
	
	public function fetchRow($id)
	{
		$row			= parent::fetchRow("id=$id");
		$row['flag']	= explode(',',$row['flag']);
		return $row;
	}
	
	public function fetchRowById($id)
	{
		$row			= parent::fetchRow("id=$id");
		$row['flag']	= explode(',',$row['flag']);
		$row['pic']		= !empty($row['pic']) ? explode('::::::', $row['pic']) : '';
		return $row;
	}
	
	public function getTempList($list, $columns, $attr=null)
	{
		if(!is_array($list)) $list	= $list->toArray();
		foreach($list as $key=>$val)
		{
			$column		= $columns[$val['classid']];
			$aid		= $val['id'];
			$newsurl	= !empty($column['domain']) ? $column['domain'] : $attr['url'].$column['classpath'];
			$link		= $column['contentextension'] ;
			$url 		= !empty($column['domain']) ? $column['domain'] : $attr['url'].$column['classpath'];
			
			eval("\$link = \"$link\";");
			eval("\$url = \"$url\";");
			
			$list[$key]['link']			= $link;
			$list[$key]['url']			= $url;
			$list[$key]['classname']	= $column['classname'];
		}
		return $list;
	}
	
	public function insert($arg)
	{
		if ($_FILES)
		{
			if($_FILES['pic']['error']==0)
	        {
	            $website	= base::init('website')->fetchRow(array('wid'=>$arg['wid']));
				$attr		= base::init('website')->getConfig($website['domain']);
				
				$filename	= md5_file($_FILES['pic']['tmp_name']);
				$filetype	= strtolower(pathinfo($_FILES['pic']['name'],PATHINFO_EXTENSION));
				
				$dir		= $attr['fileurl'] . '/' .substr($filename,0,2).'/'.substr($filename,2,2);
				$filedir	= ROOT . $attr['arcdir'] . '/' . $dir.'/';
				$realdir	= ROOT . $attr['arcdir'] . '/' . $dir.'/'.$filename.'.'.$filetype;
				$srealdir	= ROOT . $attr['arcdir'] . '/' . $dir.'/s_'.$filename.'.'.$filetype;//缩略图路径
		
				$sfileurl	= '/'.$dir.'/s_'.$filename.'.'.$filetype;//缩略图url
				$fileurl	= '/'.$dir.'/'.$filename.'.'.$filetype;
				
				is_dir($filedir) || mdir($filedir);
				
		        $result		= move_uploaded_file($_FILES['pic']['tmp_name'], $realdir);
		        
		        //生成缩略图
		        require ROOT . 'libs/SimpleImage.php';
		
				$image	= new SimpleImage();
				$image->load($realdir);//上传的临时文件名
				$image->resizeToWidth(80);//设置宽度
				$image->save($srealdir);
		        
				$arg['pic']		= $fileurl.'::::::'.$sfileurl;
				$arg['flag']	= 'p';
	        }
		}
		$id	= parent::insert($arg);
		$word	= dict($arg['title']);
		foreach ($word as $v)
		{
			base::init('classified_tag')->insert(array('tag'=>$v['word'],'id'=>$id));
		}
		return $id;
	}
	
	public function update($arg, $where)
	{
		if ($_FILES)
		{
			if($_FILES['pic']['error']==0)
	        {
	            $website	= base::init('website')->fetchRow(array('wid'=>$arg['wid']));
				$attr		= base::init('website')->getConfig($website['domain']);
				
				$filename	= md5_file($_FILES['pic']['tmp_name']);
				$filetype	= strtolower(pathinfo($_FILES['pic']['name'],PATHINFO_EXTENSION));
				
				$dir		= $attr['fileurl'] . '/' .substr($filename,0,2).'/'.substr($filename,2,2);
				$filedir	= ROOT . $attr['arcdir'] . '/' . $dir.'/';
				$realdir	= ROOT . $attr['arcdir'] . '/' . $dir.'/'.$filename.'.'.$filetype;
				$srealdir	= ROOT . $attr['arcdir'] . '/' . $dir.'/s_'.$filename.'.'.$filetype;//缩略图路径
		
				$sfileurl	= '/'.$dir.'/s_'.$filename.'.'.$filetype;//缩略图url
				$fileurl	= '/'.$dir.'/'.$filename.'.'.$filetype;
				
				is_dir($filedir) || mdir($filedir);
				
		        $result		= move_uploaded_file($_FILES['pic']['tmp_name'], $realdir);
		        
		        //生成缩略图
		        require ROOT . 'libs/SimpleImage.php';
		
				$image	= new SimpleImage();
				$image->load($realdir);//上传的临时文件名
				$image->resizeToWidth(80);//设置宽度
				$image->save($srealdir);
		        
				$arg['pic']		= $fileurl.'::::::'.$sfileurl;
				$arg['flag'][]	= 'p';
				$arg['flag']	= join(',',$arg['flag']);
	        }
		}

		parent::update($arg,$where);
		base::init('classified_tag')->delete(array('id'=>$where['id']));
		if (isset($arg['title']))
		{
			$word	= dict($arg['title']);
			foreach ($word as $v)
			{
				base::init('classified_tag')->insert(array('tag'=>$v['word'],'id'=>$arg['id']));
			}
		}
	}
	
	public function delete($where)
	{
		base::init('classified_tag')->delete(array('id'=>$where['id']));
	}
}